//
//  Cinema.m
//  Project1
//
//  Created by 黄珂耀 on 15/7/17.
//  Copyright (c) 2015年 黄珂耀. All rights reserved.
//

#import "Cinema.h"
#import "jsonAnalysis.h"
#import "common.h"
#import "CinemaCell.h"
#import "CinemaModal.h"

@interface Cinema ()
{
    NSArray *_districtArray;
    
    BOOL flags[19];
}

@end

@implementation Cinema

#pragma mark - Default Operation

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        self.title = @"影院";
    }
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self _loadData];
    [self _createSubviews];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - LoadData

- (void)_loadData
{
//    _cinemaModalArray = [[NSMutableArray alloc] init];
    
    NSDictionary *districtRootDic = [jsonAnalysis initWithJsonFileName:@"district_list.json"];
    
    _districtArray = [districtRootDic objectForKey:@"districtList"];
    
    NSInteger index = [_districtArray count];
    
    NSDictionary *cinemaRootDic = [jsonAnalysis initWithJsonFileName:@"cinema_list.json"];
    
    NSArray *cinemaListArray = [cinemaRootDic objectForKey:@"cinemaList"];
    
    for (int i = 0; i < index; i++)
    {
        NSString *districtID = [_districtArray[i] objectForKey:@"id"];
        
//        NSLog(@"%@",districtID);
        
        NSMutableArray *currentArray = [[NSMutableArray alloc] init];
        
        for (NSDictionary *dic in cinemaListArray)
        {
            CinemaModal *modal = [[CinemaModal alloc] init];
            
            [modal setValuesForKeysWithDictionary:dic];
            
//            NSLog(@"%@",modal);
            
//            NSLog(@"%@",modal.districtId);

            if ([modal.districtId isEqualToString:districtID])
            {
                [currentArray addObject:modal];
                
//                NSLog(@"%@",currentArray);
            }
        }
        
        [_districtArray[i] setValue:currentArray forKey:districtID];
    }
    
//    NSLog(@"%@",_districtArray);
    
}

#pragma mark - CreateSubviews

- (void)_createSubviews
{
    _cinemaTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    [_cinemaTableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]]];
    
    _cinemaTableView.delegate = self;
    _cinemaTableView.dataSource = self;
    
    UINib *nib = [UINib nibWithNibName:@"CinemaCell" bundle:[NSBundle mainBundle]];
    
    [_cinemaTableView registerNib:nib forCellReuseIdentifier:@"cinemaCell"];
    
    [self.view addSubview:_cinemaTableView];
}

#pragma mark - CinemaTableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton *button = [[UIButton alloc] init];
//    [button setBackgroundColor:[UIColor colorWithRed:0.219 green:0.298 blue:0.310 alpha:1.000]];
    [button setBackgroundImage:[UIImage imageNamed:@"nav_bg_all"] forState:UIControlStateNormal];
    NSString *districtName = [_districtArray[section] objectForKey:@"name"];
    [button setTitle:districtName forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = section;
    
    return button;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 83;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_districtArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    return [_districtArray[section] count];
    if (flags[section] == NO)
    {
        return 0;
    }
    else
    {
        NSDictionary *dic = _districtArray[section];
        NSArray *cinemas = [dic objectForKey:[dic objectForKey:@"id"]];
        return [cinemas count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CinemaCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cinemaCell" forIndexPath:indexPath];
    
    cell.modal = [_districtArray[indexPath.section] objectForKey:[_districtArray[indexPath.section] objectForKey:@"id"]][indexPath.row];
    
    return cell;
}

#pragma mark - Actions

- (void)buttonAction:(UIButton *)button
{
    NSInteger index = button.tag;
    flags[index] = !flags[index];
    
    NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:index];
    [_cinemaTableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
